package com.chuan.it_market.controller;

import com.chuan.it_market.consts.GoodsConsts;
import com.chuan.it_market.pojo.entity.Seller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class BaseController {

    Logger logger= LoggerFactory.getLogger(BaseController.class);
    protected String uploadFile(String uploadDir, MultipartFile f){
        String realPath= GoodsConsts.getUploadPath();
        String uploadPath=uploadDir+ UUID.randomUUID()+"."+f.getContentType().split("/")[1];
        File file =new File(realPath+uploadPath);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try{
            if(!f.isEmpty()){
                if(f.getSize()>0){
                    f.transferTo(file);
                    logger.info("图片已存到"+realPath+uploadPath);
                    return uploadPath;
                }
            }
        }catch(IOException e){
            e.printStackTrace();

        }
        return GoodsConsts.NO_UPLOAD_IMG;

    }

    protected HttpServletRequest getRequest(){
        return((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    protected HttpSession getSession(){
        return this.getRequest().getSession();
    }
    protected Integer getUsreId(){
        return (Integer) this.getSession().getAttribute("userId");
    }
    protected String getUserPassword(){return (String) this.getSession().getAttribute("userPassword");}
}
